package com.ranjiaqi.thread01;

/**
 * @ClassName Resource
 * @Description TODO
 * @Author hp
 * @Date 2021/6/29 9:10
 * @Version 1.0
 **/
public class Resource {
    private  static Resource r = new Resource();
    private  boolean flag;
    private  int count;
    public  synchronized  void  produce(){
        if (flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else {
            System.out.println("生产第...."+(++count)+"个");
            flag = true;
            this.notify();
        }


    }
    public  synchronized  void custom(){
        if (!flag) {
            try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); }
        }else {
            System.out.println("消费第" + count + "个");
            flag = false;
            this.notify();
        }
    }

    private Resource(){


    }
    public static Resource getInstance(){
        return  r;
    }
}
